/**
 * 全局变量
 */
var deptDatas;
var isClick = true;
var deptIdStatic = 1;
var statusStatic = 0;
$(function(){
	activeNav($("#person-book"));
	
	var setting = {
		data: {
			simpleData: {
				enable: true
			}
		},
		async:{
			enable: true,
			dataType: "json",
			type: "post",
			url: ctx + "/admin/qiye/dept/ajaxDepts",
			dataFilter: ajaxDataFilter
		},
		view: {
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
			selectedMulti: false
		},
		callback : {
			beforeAsync:function(){
				$("#treeBody").mask("加载中...");
			},
			onAsyncSuccess: function(){
				$("#treeBody").unmask();
			},
			onAsyncError: function(){
				$("#treeBody").unmask();
			},
			onClick: zTreeOnClick
		}
	};
	$.fn.zTree.init($("#deptTree"), setting);
	
	//默认加载的用户列表
	ajaxUserList(1, 1, 0);
	
	$("#dept-cancel").click(function () {
		clearDept();
	});
	
	$("#dept-close").click(function () {
		clearDept();
	});
	
	$("#person-cancel").click(function(){
		clearPerson();
	});
	
	$("#person-close").click(function(){
		clearPerson();
	});
	
	$('#personWeixinid').blur(function(){
		checkTipsBlur();
	});
	$('#personMobile').blur(function(){
		checkTipsBlur();
	});
	$('#personEmail').blur(function(){
		checkTipsBlur();
	});
	
	$('#status').change(function(){
		statusStatic = $(this).val();
		ajaxUserList(deptIdStatic, 1, statusStatic);
	});
	
	$("#find-cancel").click(function(){
		clearFind();
	});
	
	$("#find-close").click(function(){
		clearFind();
	});
	
	$("#send-cancel").click(function(){
		clearSend();
	});
	
	$("#send-close").click(function(){
		clearSend();
	});
	
	$("#import-cancel").click(function(){
		clearImport();
	});
	
	$("#import-close").click(function(){
		clearImport();
	});
});

/**
 * 异步加载数据过滤
 * @param treeId
 * @param parentNode
 * @param responseData
 * @returns {Array}
 */
function ajaxDataFilter(treeId, parentNode, responseData) {
    var rd = [];
	if (responseData) {
		 for(var i =0; i < responseData.department.length; i++) {
			 var obj = new Object();
			 obj.id = responseData.department[i].id;
			 obj.name = responseData.department[i].name;
			 obj.pId = responseData.department[i].parentid;
			 obj.iconSkin = 'deptIcon';
			 rd.push(obj);
		 }
    }
	deptDatas = rd;
    return rd;
}
/**
 * 树获取焦点
 * @param treeId
 * @param treeNode
 */
function addHoverDom(treeId, treeNode) {
	var sObj = $("#" + treeNode.tId + "_span");
	if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
	
	var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
			+ "' title='新增' onfocus='this.blur();'></span>";
	
	var editStr = "<span class='button edit' id='editBtn_" + treeNode.tId
			+ "' title='重命名' onfocus='this.blur();'></span>";
	
	var removeStr = "<span class='button remove' id='removeBtn_" + treeNode.tId
			+ "' title='删除' onfocus='this.blur();'></span>";
	if(treeNode.isParent){
		sObj.after(editStr);
		sObj.after(addStr);
	}else{
		sObj.after(removeStr);
		sObj.after(editStr);
		sObj.after(addStr);
	}
	var addBtn = $("#addBtn_" + treeNode.tId);
	if (addBtn) $(addBtn).click(function(){
		isClick = false;
		$('#dept-title').text("新建部门");
		$("#dept-body").show();
		$('#deptName').focus();
		
		var path = treeNode.name;
		var p = treeNode.getParentNode();
		while (p){
			path = p.name + " &raquo; " + path;
			p = p.getParentNode();
		}
		$('#parentDept').html(path);
		
		$("#dept-save").click(function(){
			var deptName = $('#deptName').val().trim();
			if(deptName.length == 0){
				$('#deptName').focus();
				return;
			}
			var datas = {name : deptName, parentid : treeNode.id};
			ajaxPostDept(ctx + '/admin/qiye/dept/ajaxCreate', datas, treeId);
		});
	});
	
	var editBtn = $("#editBtn_" + treeNode.tId);
	if (editBtn) $(editBtn).click(function(){
		isClick = false;
		$('#dept-title').text("重命名");
		$("#dept-body").show();
		$('#deptName').focus();
		$('#deptName').val(treeNode.name);
		
		var path;
		var p = treeNode.getParentNode();
		while (p){
			if(path){
				path = p.name + " &raquo; " + path;
			}else{
				path = p.name;
			}
			p = p.getParentNode();
		}
		$('#parentDept').html(path);
		
		$("#dept-save").click(function(){
			var deptName = $('#deptName').val().trim();
			if(deptName.length == 0){
				$('#deptName').focus();
				return;
			}
			var datas = {id:treeNode.id, name : deptName };
			ajaxPostDept(ctx + '/admin/qiye/dept/ajaxUpdate', datas, treeId);
		});
	});
	
	var removeBtn = $("#removeBtn_" + treeNode.tId);
	if (removeBtn) removeBtn.bind("click", function(){
		isClick = false;
		$.confirm({
			message: "<div>确认删除部门？</div><div>没有子部门且没有成员的部门才可以被删除。</div>",
			callback: function(){
				ajaxPostDept(ctx + '/admin/qiye/dept/ajaxDelete', { id:treeNode.id } , treeId);
			}
		});
	});
};
/**
 * 树焦点移开
 * @param treeId
 * @param treeNode
 */
function removeHoverDom(treeId, treeNode) {
	if($("#addBtn_" + treeNode.tId)){
		$("#addBtn_" + treeNode.tId).unbind().remove();
	}
	if($("#editBtn_" + treeNode.tId)){
		$("#editBtn_" + treeNode.tId).unbind().remove();
	}
	if($("#removeBtn_" + treeNode.tId)){
		$("#removeBtn_" + treeNode.tId).unbind().remove();
	}
};
/**
 * zTree点击事件
 */
function zTreeOnClick(event, treeId, treeNode, clickFlag){
	var treeObj = $.fn.zTree.getZTreeObj(treeId);
	treeObj.cancelSelectedNode();
	if(isClick){
		if(treeNode.isParent){
			if(!treeNode.open){
				treeObj.expandNode(treeNode, true, false);
			}else{
				treeObj.expandNode(treeNode, false, false);
			}
		}
		deptIdStatic = treeNode.id;//给全局部门ID赋值
		ajaxUserList(treeNode.id, 1, 0 );
		var title = '';
		if(treeNode.level != 0){
			var path = treeNode.name;
			var p = treeNode.getParentNode();
			while (p){
				if(p.level != 0){
					path = p.name + " &raquo; " + path;
				}
				p = p.getParentNode();
			}
			title = "【" + path + "】";
		}
		$('#deptTableTitle').html(title);
	}
	isClick = true;
}

/**
 * 隐藏部门弹出框
 */
function clearDept () {
	$("#dept-body").hide();
	$('#deptName').val('');
	$("#dept-save").unbind();
	$('#parentDept').html('');
}
/**
 * 提交部门信息
 */
function ajaxPostDept(url, datas, treeId){
	$.ajax({
		async : false,
		cache : false,
		dataType : 'JSON',
		type : 'POST',
		url : url,
		data : datas,
		beforeSend : function(){
			$('#dept-form').mask("正在提交...");
		},
		success : function(data) {
			$('#dept-form').unmask();
			$.alert({message: data.errmsg});
			clearDept();
			var treeObj = $.fn.zTree.getZTreeObj(treeId);
			treeObj.reAsyncChildNodes(null, "refresh");
		},
		error : function() {
			$('#dept-form').unmask();
			$.alert({message: "操作异常，请稍候再试！"});
		}
	});
}
/**
 * 获取用户列表
 * @param deptId
 * @param fetchChild
 * @param status
 */
function ajaxUserList(deptId, fetchChild, status){
	$('#status').val(status);
	$.ajax({
		async : true,
		cache : false,
		dataType : 'JSON',
		type : 'POST',
		url : ctx + '/admin/qiye/dept/ajaxUserList',
		data : {deptId: deptId, fetchChild: fetchChild, status: status},
		beforeSend : function(){
			$('#tableBody').mask("成员列表加载中...");
		},
		success : function(data) {
			$('#tableBody').unmask();
			if(data.errcode != 0){
				$.alert({message: data.errmsg});
				return false;
			}else{
				var html = new Array();
				if(data.userlist.length > 0){
					$(data.userlist).each(function(i, user){
						var status = user.status;
						switch(status){
							case 1:
								status = "已关注";
								break;
							case 2:
								status = "已冻结";
								break;
							case 4:
								status = "未关注 ";
								break;
							default:
								status = "";
								break;
						}
						var avatar = user.avatar ? user.avatar : 'https://res.mail.qq.com/bizmp/zh_CN/images/dev/icon_avatar_default.png';
						var position = user.position ? user.position : '';
						var mobile = user.mobile ? user.mobile : '';
						var email = user.email ? user.email : '';
						html.push('<tr>');
						html.push('<td style="text-align: center;"><input type="checkbox" name="id" value="' + user.userid + '" /></td>');
						html.push('<td style="text-align: right;height:30px;line-height:30px;"><img style="" height="30" width="30" src="' + avatar + '"/></td>');
						html.push('<td>' + user.name + '</td>');
						html.push('<td style="text-align: center;">' + user.userid + '</td>');
						html.push('<td style="text-align: center;">' + position + '</td>');
						// html.push('<td style="text-align: center;">' + mobile + '</td>');
						// html.push('<td style="text-align: center;">' + email + '</td>');
						html.push('<td style="text-align: center;">' + status + '</td>');
						html.push('<td style="text-align: center;">');
						html.push('<a class="button border-blue button-little" onclick="updatePerson(\'' + user.userid + '\')" href="javascript:void(0);">修改</a> ');
						html.push('<a class="button border-yellow button-little" onclick="deletePerson(\'' + user.userid + '\')" href="javascript:void(0);">删除</a> ');
						html.push('<a class="button border-green button-little" onclick="findPerson(\'' + user.userid + '\')" href="javascript:void(0);">查看</a>');
						html.push('</td>');
						html.push('</tr>');
					});
				}else{
					html.push('<tr><td colspan="9" style="text-align: center;">没有成员</td></tr>');
				}
				$("#user-table tr:gt(0)").remove();
				$('#user-table').append(html.join(''));
			}
		},
		error : function() {
			$('#tableBody').unmask();
			$.alert({message: "操作异常，请稍候再试！"});
		}
	});
}

/**
 * 新增成员
 */
function addPerson(){
	$('#person-body').show();
	getPersonTree();
	$('#personName').focus();
	$('#person-title').text('新增成员');
	personSaveFun(ctx + '/admin/qiye/dept/ajaxUserCreate', 1, 0);
}
/**
 * 修改成员
 * @param id
 */
function updatePerson(id){
	$('#person-body').show();
	getPersonTree();
	$('#personName').focus();
	$('#person-title').text('修改成员');
	$.ajax({
		async : false,
		cache : false,
		dataType : 'JSON',
		type : 'POST',
		url : ctx + '/admin/qiye/dept/ajaxUserDetail',
		data : {userid: id},
		beforeSend : function(){
			$('#person-form').mask("加载中...");
		},
		success : function(data) {
			$('#person-form').unmask();
			if(data.errcode != 0){
				$.alert({message: data.errmsg});
			}else{
				$('#personName').val(data.name);
				$('#personUserid').val(data.userid);
				$('#personWeixinid').val(data.weixinid);
				$('#personMobile').val(data.mobile);
				$('#personEmail').val(data.email);
				$('#personPosition').val(data.position);
				
				var treeObj = $.fn.zTree.getZTreeObj("personTree");
				$(data.department).each(function(i, id){
					var nodes = treeObj.getNodesByParam("id", id, null);
					treeObj.checkNode(nodes[0], true, false, true);
				});
			}
		},
		error : function() {
			$('#person-form').unmask();
			$.alert({message: "操作异常，请稍候再试！"});
		}
	});
	personSaveFun(ctx + '/admin/qiye/dept/ajaxUserUpdate',deptIdStatic, statusStatic);
}
/**
 * 删除成员
 * @param id
 */
function deletePerson(id){
	$.confirm({
		message: "确认删除该成员？",
		callback: function(){
			ajaxPostUser(ctx + '/admin/qiye/dept/ajaxUserDelete', { id: id }, deptIdStatic, statusStatic);
		}
	});
}
/**
 * 批量删除成员
 */
function deleteBatchPerson(){
	var ids = [];
	$(".table input[name=id]:checked").each(function(){
		var id = $(this).val();
		ids.push(id);
	});
	if(ids.length==0){
		$.alert({message:"请先勾选记录再批量删除！"});
	}else{
		$.confirm({
			message:"确定要删除这些记录？",
			callback:function(){
				ajaxPostUser(ctx + '/admin/qiye/dept/ajaxUserBatchDelete', { useridlist: ids.join(',') }, deptIdStatic, statusStatic);
			}
		});
	}
}
/**
 * 隐藏成员弹出框
 */
function clearPerson(){
	$('#person-body').hide();
	$('#person-save').unbind();
	$('#checkTips').removeClass('text-red');
	$('#deptTips').hide();
	$('#personName').val('');
	$('#personUserid').val('');
	$('#personWeixinid').val('');
	$('#personMobile').val('');
	$('#personEmail').val('');
	$('#personPosition').val('');
	var treeObj = $.fn.zTree.getZTreeObj("personTree");
	treeObj.checkAllNodes(false);
}
/**
 * 微信号/手机号/邮箱不能同时为空验证
 */
function checkTipsBlur(){
	var b = false;
	var weixinid = $('#personWeixinid').val().trim();
	var mobile = $('#personMobile').val().trim();
	var email = $('#personEmail').val().trim();
	if(weixinid.length == 0 && mobile.length == 0 && email.length == 0){
		$('#checkTips').addClass('text-red');
		$('#personWeixinid').focus();
	}else{
		$('#checkTips').removeClass('text-red');
		b = true;
	}
	return b;
}
/**
 * 成员表单验证
 * @returns {Boolean}
 */
function checkPersonAll(){
	var name = $('#personName').val().trim();
	var userid = $('#personUserid').val().trim();
	var weixinid = $('#personWeixinid').val().trim();
	var mobile = $('#personMobile').val().trim();
	var email = $('#personEmail').val().trim();
	var nameBool = false;
	if(name.length == 0){
		$('#personName').focus();
	}else{
		nameBool = true;
	}
	var useridBool = false;
	if(userid.length == 0){
		$('#personUserid').focus();
	}else{
		useridBool = true;
	}
	var checkBool = checkTipsBlur();
	var treeBool = false;
	var treeObj = $.fn.zTree.getZTreeObj("personTree");
	var nodes = treeObj.getCheckedNodes(true);
	if(nodes.length == 0){
		$('#deptTips').show();
	}else{
		$('#deptTips').hide();
		treeBool = true;
	}
	return (nameBool && useridBool && checkBool && treeBool);
}
/**
 * 加载成员信息中的树
 */
function getPersonTree(){
	var setting = {
		data: {
			simpleData: {
				enable: true
			}
		},
		check: {
			enable: true,
			chkStyle: "checkbox",
			chkboxType: { "Y": "", "N": "" }
		},
		callback : {
			onClick: function(event, treeId, treeNode, clickFlag){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				treeObj.cancelSelectedNode();
				if(treeNode.isParent){
					if(!treeNode.open){
						treeObj.expandNode(treeNode, true, false);
					}else{
						treeObj.expandNode(treeNode, false, false);
					}
				}
			},
			onCheck: personZTreeOnCheck
		}
	};
	$.fn.zTree.init($("#personTree"), setting, deptDatas);
}
/**
 * 成员信息树Checked事件
 * @param event
 * @param treeId
 * @param treeNode
 */
function personZTreeOnCheck(event, treeId, treeNode){
	var treeObj = $.fn.zTree.getZTreeObj(treeId);
	var nodes = treeObj.getCheckedNodes(true);
	if(nodes.length == 0){
		$('#deptTips').show();
	}else{
		$('#deptTips').hide();
	}
}
/**
 * 提交成员信息
 */
function ajaxPostUser(url, datas, deptId, status){
	$.ajax({
		async : false,
		cache : false,
		dataType : 'JSON',
		type : 'POST',
		url : url,
		data : datas,
		beforeSend : function(){
			$('#person-form').mask("正在提交...");
		},
		success : function(data) {
			$('#person-form').unmask();
			$.alert({message: data.errmsg});
			ajaxUserList(deptId, 1, status);
			clearPerson();
		},
		error : function() {
			$('#person-form').unmask();
			$.alert({message: "操作异常，请稍候再试！"});
		}
	});
}
/**
 * 成员信息保存按钮事件
 * @param url
 */
function personSaveFun(url, deptId, status){
	$('#person-save').click(function(){
		var name = $('#personName').val().trim();
		var userid = $('#personUserid').val().trim();
		var weixinid = $('#personWeixinid').val().trim();
		var mobile = $('#personMobile').val().trim();
		var email = $('#personEmail').val().trim();
		var position = $('#personPosition').val().trim();
		if(checkPersonAll() && formValid($('#person-form'))){
			var department = [];
			var treeObj = $.fn.zTree.getZTreeObj("personTree");
			var nodes = treeObj.getCheckedNodes(true);
			$(nodes).each(function(i,node){
				department.push(node.id);
			});
			var datas = {
					name: name, 
					userid: userid,
					weixinid: weixinid,
					mobile: mobile,
					email: email,
					position: position,
					department: department.join(",")
				};
			ajaxPostUser(url, datas, deptId, status);
		}
	});
}
/**
 * 点击查看按钮方法
 * @param id
 */
var findStatus = null;
var findUserid = null;
var findMobile = null;
var findWeixinid = null;
var findName = null;
function findPerson(id){
	$('#find-body').show();
	$.ajax({
		async : false,
		cache : false,
		dataType : 'JSON',
		type : 'POST',
		url : ctx + '/admin/qiye/dept/ajaxUserDetail',
		data : {userid: id},
		beforeSend : function(){
			$('#find-form').mask("加载中...");
		},
		success : function(data) {
			$('#find-form').unmask();
			if(data.errcode != 0){
				$.alert({message: data.errmsg});
			}else{
				findStatus = data.status;
				findUserid = data.userid;
				findMobile = data.mobile;
				findWeixinid = data.weixinid;
				findName = data.name;
				$('#find-content').html(getFindHtml(data));
				if(data.status == 1){
					$('#find-invite').attr("disabled",true);
				}else{
					$('#find-invite').attr("disabled",false);
				}
				if(data.status == 2){
					$('#find-invite').attr("disabled",true);
					$('#find-disabled').text("启用");
				}else{
					$('#find-disabled').text("冻结");
				}
			}
		},
		error : function() {
			$('#find-form').unmask();
			$('#find-disabled').text("冻结");
			$('#find-invite').attr("disabled",true);
			$('#find-disabled').attr("disabled",true);
			$.alert({message: "操作异常，请稍候再试！"});
		}
	});
}
/**
 * 邀请关注
 */
function inviteUser(){
	var status = findStatus;
	var mobile = findMobile;
	var weixinid = findWeixinid;
	var name = findName;
	var userid = findUserid;
	clearFind();
	if(status == 1 || status == 2){
		$.alert({message: '非法操作！'});
		return;
	}
	if(mobile == null && weixinid == null){
		$.confirm({
			message: '是否通过邮件发送二维码，邀请' + name + '关注？',
			callback: function(){
				findAjaxPost(ctx + '/admin/qiye/dept/ajaxUserInvite', {userid: userid});
			}
		});
	}else{
		$('#send-body').show();
		$('#send-user').text(name);
		$('#send-save').click(function(){
			var inviteTips = $('#inviteTips').val();
			var data = {userid: userid, invite_tips: inviteTips}
			clearSend();
			findAjaxPost(ctx + '/admin/qiye/dept/ajaxUserInvite', data);
		});
	}
}
/**
 * 隐藏查看界面
 */
function clearFind(){
	findStatus = null;
	findUserid = null;
	findMobile = null;
	findWeixinid = null;
	findName = null;
	$('#find-body').hide();
	$('#find-content').html('');
}
/**
 * 微信推送界面隐藏
 */
function clearSend(){
	$('#send-body').hide();
	$('#inviteTips').val('');
	$('#send-user').text('');
	$("#send-save").unbind();
}
/**
 * 禁用或启用用户
 */
function disabledUser(){
	var name = findName;
	var userid = findUserid;
	var status = findStatus;
	clearFind();
	if(status == 2){
		findAjaxPost(ctx + '/admin/qiye/dept/ajaxUserEnable', {userid: userid, enable: 1});
	}else{
		$.confirm({
			message: '你确定要冻结' + name + '吗？',
			callback: function(){
				findAjaxPost(ctx + '/admin/qiye/dept/ajaxUserEnable', {userid: userid, enable: 0});
			}
		});
	}
	
}
/**
 * 获取查看界面html
 * @param user
 * @returns
 */
function getFindHtml(user){
	var html = new Array();
	var status = user.status;
	switch(status){
		case 1:
			status = "已关注";
			break;
		case 2:
			status = "已冻结";
			break;
		case 4:
			status = "未关注 ";
			break;
		default:
			status = "";
			break;
	}
	var avatar = user.avatar ? user.avatar : 'https://res.mail.qq.com/bizmp/zh_CN/images/dev/icon_avatar_default.png';
	html.push('<div class="cc_header">');
	html.push('<img height="80" width="80" src="' + avatar + '">');
	html.push('<div class="cc_detail">');
	html.push('<p class="c_name ellipsis">' + user.name + '</p>');
	html.push('<p class="e_name ellipsis">' + user.userid + '</p>');
	html.push('<span class="freeze">' + status + '</span>');
	html.push('</div></div>');
	html.push('<div class="cc_content">');
	if(user.mobile){
		html.push('<div class="item"><span class="label">手机</span><p class="info">' + user.mobile + '</p></div>');
	}
	if(user.weixinid){
		html.push('<div class="item"><span class="label">微信</span><p class="info">' + user.weixinid + '</p></div>');
	}
	if(user.email){
		html.push('<div class="item"><span class="label">邮箱</span><p class="info">' + user.email + '</p></div>');
	}
	if(user.position){
		html.push('<div class="item"><span class="label">职位</span><p class="info">' + user.position + '</p></div>');
	}
	if(user.department.length > 0){
		html.push('<div class="item"><span class="label">部门</span>');
		$(user.department).each(function(i , department){
			var deptName;
			$(deptDatas).each(function(i , dept){
				if(department == dept.id){
					deptName = dept.name;
					return;
				}
			});
			html.push('<p class="info">' + deptName + '</p>');
		});
		html.push('</div>');
	}
	html.push('</div>');
	return html.join('');
}
function findAjaxPost(url, data){
	$.ajax({
		async : false,
		cache : false,
		dataType : 'JSON',
		type : 'POST',
		url : url,
		data : data,
		beforeSend : function(){
			$('.admin').mask("正在发送请求...");
		},
		success : function(data) {
			$('.admin').unmask();
			if(data.errcode == 0){
				if(data.type == 1){
					$.alert({message: "发送到微信或者手机成功"});
				}
				if(data.type == 2){
					$.alert({message: "邮件发送成功"});
				}
				ajaxUserList(deptIdStatic, 1, statusStatic);
			}else{
				$.alert({message: data.errmsg});
			}
		},
		error : function() {
			$('.admin').unmask();
			$.alert({message: "操作异常，请稍候再试！"});
		}
	});
}


/**
 * 批量导入
 */
var uploader;
function importBatchPerson(){
	$('#import-body').show();
	uploader = WebUploader.create({
		auto : false,
		swf : ctx + 'static/plugins/webuploader/Uploader.swf',
		server : ctx + "/admin/qiye/dept/import",
		pick: { id : '#upload-file-btn', multiple : false },
		fileVal : 'file',
		duplicate : true,
		method : 'POST',
		accept : {
		    title : '选择文件(*.xlsx,*.xls)',
		    extensions : 'xlsx,xls',
		    mimeTypes : '*'
		}
	});
	
	uploader.on( 'error', function( type ) {
		switch (type) {
			case 'Q_TYPE_DENIED':
				$.alert({message:'请选择正确的文件类型！（只支持*.xlsx,*.xls）'});
				break;
			default:
				break;
		}
	});
	
	uploader.on( 'startUpload', function() {
		$('#import-form').mask('正在导入...');
	});
	
	uploader.on( 'fileQueued', function( file  ) {
		uploaderFlag = true;
		var files = uploader.getFiles();
		if(files.length > 1){
			uploader.reset();
			uploader.addFiles( file );
		}
		$("#upload-file-name").text(file.name);
	});
	
	uploader.on( 'uploadAccept', function(object , ret ) {
		clearImport();
		$.alert({message:ret.msg});
		$('#import-form').unmask();
		var treeObj = $.fn.zTree.getZTreeObj("deptTree").reAsyncChildNodes(null, "refresh")
		ajaxUserList(1, 1, 0);
	});
	
	uploader.on( 'uploadFinished', function() {
	});
	
	$('#import-save').click(function(){
		uploader.upload();
	});
	
	$('#downloadTemplate').click(function(){
		downloadTemplate();
	});
}
/**
 * 批量导入重置
 */
function clearImport(){
	$('#import-body').hide();
	$('#import-save').unbind();
	$('#downloadTemplate').unbind();
	uploader.destroy();
	$("#upload-file-name").text('');
}

function downloadTemplate(){
	var uuid = $.uuid();
	var form = '<form id="' + uuid + '" action="' + ctx + '/admin/qiye/dept/download" method="post"></form>'
	$('body').append(form);
	$('#' + uuid).submit();
	$('#' + uuid).remove();
}